package mn.slide.dailynews.model;

import java.util.Date;

import com.j256.ormlite.dao.ForeignCollection;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;

public class TvDay
{
	public static final String			NAME_FIELD_NAME	= "name";
	public static final String			TV_FIELD_NAME	= "tv_id";
	public static final String			DATE_FIELD_NAME	= "date";

	@DatabaseField(generatedId = true)
	public int							id;

	@DatabaseField(uniqueIndexName = "unique_tv_date_day_name")
	public String						name;

	@DatabaseField(uniqueIndexName = "unique_tv_date_day_name")
	public Date							date;

	@DatabaseField(canBeNull = false, foreign = true, uniqueIndexName = "unique_tv_date_day_name")
	public Tv							tv;

	@ForeignCollectionField(eager = true)
	public ForeignCollection<TvDayItem>	tvDayItems;

	public TvDay()
	{

	}

	public TvDay(String name, Date date, Tv tv)
	{
		this.name = name;
		this.date = date;
		this.tv = tv;
	}

	public TvDayItem getCurrentTvDayItem(){
		Date now = new Date();
		for (TvDayItem tvDayItem : this.tvDayItems)
		{
			if (tvDayItem.getStartDate().getTime() < now.getTime() && now.getTime() < tvDayItem.getEndDate().getTime())
				return tvDayItem;
		}
		return null;
	}
	public TvDayItem getNextTvDayItem(TvDayItem currentTvDayItem)
	{
		if (currentTvDayItem == null)
			return null;
		boolean current = false;
		for (TvDayItem tvDayItem : tvDayItems){
			if (current)
				return tvDayItem;
			if (tvDayItem.id == currentTvDayItem.id)
				current = true;
		}
		return null;
	}
	public int getId()
	{
		return id;
	}

	public void setId(int id)
	{
		this.id = id;
	}

	public Date getDate()
	{
		return date;
	}

	public void setDate(Date date)
	{
		this.date = date;
	}

	public Tv getTv()
	{
		return tv;
	}

	public void setTv(Tv tv)
	{
		this.tv = tv;
	}

	public ForeignCollection<TvDayItem> getTvDayItems()
	{
		return tvDayItems;
	}

	public void setTvDayItems(ForeignCollection<TvDayItem> tvDayItems)
	{
		this.tvDayItems = tvDayItems;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

}
